package com.leyou.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.mapper.SkuMapper;
import com.leyou.pojo.Sku;
import com.leyou.service.SkuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {
    /**
     * 下单后删减库存
     * @param cartMap
     */
    @Override
    @Transactional
    public void deductStock(Map<Long, Integer> cartMap) {
        for (Map.Entry<Long, Integer> entry : cartMap.entrySet()) {
            Long id = entry.getKey();
            Integer num = entry.getValue();
            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("num", num);
            this.baseMapper.deductStock(map);
        }
    }
//    private static final String DEDUCT_STOCK_STATEMENT = "com.leyou.mapper.SkuMapper.deductStock";
//    @Override
//    @Transactional
//    public void deductStock(Map<Long, Integer> cartMap) {
//        System.out.println("cartMap = " + cartMap);
//        executeBatch(sqlSession -> {
//            for (Map.Entry<Long, Integer> entry : cartMap.entrySet()) {
//                // 准备参数
//                Map<String,Object> param = new HashMap<>();
//                param.put("id", entry.getKey());
//                param.put("num", entry.getValue());
//                System.out.println("param = " + param);
//                // 编译statement，namespace.statementId
//                sqlSession.update(DEDUCT_STOCK_STATEMENT, param);
//            }
//            // 刷新
//            sqlSession.flushStatements();
//        });
//    }
}